package zhongche.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateUtil {

    private static Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);

    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DASH = "-";
    private static final String YEAR_PREFIX = "20";
    private static final String COLON = ":";

    /**
     * 日期转时间戳
     * @param dateString 210716153626 = 21年07月16号15时36分26秒
     * @return
     */
    public static Long transDateStrToStamp(String dateString){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
        String dataStr = YEAR_PREFIX+dateString.substring(0,2)+DASH+dateString.substring(2,4) + DASH
                + dateString.substring(4,6) + " " +dateString.substring(6,8)+COLON+dateString.substring(8,10)
                + COLON + dateString.substring(10,12);
        try {
            Date parse = simpleDateFormat.parse(dataStr);
            return parse.getTime();
        } catch (ParseException e) {
            LOGGER.info(dateString+ " :解析失败！！！");
            return null;
        }


    }
}
